package com.fooddelivery.Service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fooddelivery.Common.Execption.BusinessException;
import com.fooddelivery.Common.Execption.ErrorCode;
import com.fooddelivery.Common.responseVO.LoginBackVO;
import com.fooddelivery.Entity.User;
import com.fooddelivery.Mapper.UserMapper;
import com.fooddelivery.Service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sunwenyuan
 * @since 2025-08-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public boolean save(User user) {

        LocalDateTime now = LocalDateTime.now();
        user.setCreateTime(now);
        user.setStatus(1);
        user.setRoleId(2);
        user.setIsValid("Y");
//        if (dish.getUpdateUser() == null) {
//            dish.setUpdateUser(1L);
//        }
//        if (dish.getCreateUser() == null) {
//            dish.setCreateUser(1L);
//        }
        if (user.getUsername() == null) {
            throw new BusinessException(ErrorCode.USER_USERNAME_NULL);
        }
        try {
            userMapper.save(user);
        } catch (DataIntegrityViolationException e) {
            throw new BusinessException(ErrorCode.USER_ALREADY_EXISTS);
        }
        return true;
    }

    @Override
    public LoginBackVO login(User user) {
       return userMapper.login(user);
    }


}
